package com.tanhua.server.controller;

import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.Answers;
import com.tanhua.server.vo.SoulVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/testSoul")
@Slf4j
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;

    @GetMapping
    public ResponseEntity<List<Answers>> queryAnswers(){
        try {
            List<Answers> answers=this.testSoulService.getSoulQuestions();
            if (null!=answers){
                return ResponseEntity.ok(answers);
            }
        } catch (Exception e) {
            log.error("获取问卷出错",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @PostMapping
    public ResponseEntity<String> testSoul(@RequestBody Map<String,List<Map<String,String>>> param){
        try {
            List<Map<String, String>> answers = param.get("answers");
            String s = this.testSoulService.doTestSoul(answers);
            if(null!=s){
                return ResponseEntity.ok(s);
            }
        } catch (Exception e) {
            log.error("提交问卷出错",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 生成灵魂报告表
     *
     * @param id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<SoulVo> querySoul(@PathVariable("id") String id) {
        try {
            SoulVo soulVo = testSoulService.querySoul(id);

            if (null != soulVo) {
                return ResponseEntity.ok(soulVo);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
